import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EnglishWordDeduplicationWithNewNumber {

    public static void main(String[] args) {
        String inputFilePath = "input.txt";
        String outputFilePath = "output.txt";
        Pattern englishWordPattern = Pattern.compile("[a-zA-Z]+");

        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {

            Map<String, String> uniqueLines = new LinkedHashMap<>();
            String line;

            while ((line = reader.readLine()) != null) {
                // 去除原始序号
                String lineWithoutNumber = line.replaceFirst("^\\d+\\.", "").trim();

                Matcher matcher = englishWordPattern.matcher(lineWithoutNumber);
                String firstEnglishWord = null;
                if (matcher.find()) {
                    firstEnglishWord = matcher.group().toLowerCase();
                }

                if (firstEnglishWord != null && !uniqueLines.containsKey(firstEnglishWord)) {
                    uniqueLines.put(firstEnglishWord, lineWithoutNumber);
                } else if (firstEnglishWord == null && !uniqueLines.containsValue(lineWithoutNumber)) {
                    uniqueLines.put(lineWithoutNumber, lineWithoutNumber);
                }
            }

            // 将去重后的行转换为列表，以便重新排序
            List<String> sortedLines = new ArrayList<>(uniqueLines.values());

            // 重新排序（可以根据需要添加排序逻辑）
            // 例如，按字母顺序排序： Collections.sort(sortedLines);

            // 写入文件并添加新的序号
            int count = 1;
            for (String sortedLine : sortedLines) {
                writer.write(count + "." + sortedLine);
                writer.newLine();
                count++;
            }

            System.out.println("去重并重新排序后的文本已写入到 " + outputFilePath);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
